package io.renren.modules.shippinginformation.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.renren.modules.logisticstracking.entity.LogisticsTrackingEntity;
import io.renren.modules.logisticstracking.service.LogisticsTrackingService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.waybillorder.entity.WaybillOrderEntity;
import io.renren.modules.waybillorder.service.WaybillOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.shippinginformation.entity.ShippingInformationEntity;
import io.renren.modules.shippinginformation.service.ShippingInformationService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 配送单信息
 *
 * @author cyx
 * @email 714529684@qq.com
 * @date 2022-02-25 10:59:01
 */
@RestController
@RequestMapping("generator/shippinginformation")
public class ShippingInformationController {
    @Autowired
    private ShippingInformationService shippingInformationService;
    @Autowired
    private LogisticsTrackingService logisticsTrackingService;
    @Autowired
    private WaybillOrderService waybillOrderService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:shippinginformation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = shippingInformationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:shippinginformation:info")
    public R info(@PathVariable("id") Long id){
		ShippingInformationEntity shippingInformation = shippingInformationService.getById(id);

        return R.ok().put("shippingInformation", shippingInformation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    @RequiresPermissions("generator:shippinginformation:save")
    public R save(@RequestBody ShippingInformationEntity shippingInformation){
        shippingInformation.setDeliverystatus(1);
        shippingInformation.setCreateby(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername());
        shippingInformation.setCreatetime(new Date());
        boolean save = shippingInformationService.save(shippingInformation);

        if (save){
            LogisticsTrackingEntity logisticsTrackingEntity = new LogisticsTrackingEntity();
            logisticsTrackingEntity.setWaybillno(shippingInformation.getId());
            logisticsTrackingEntity.setCreatetime(new Date());
            QueryWrapper<WaybillOrderEntity> waybillOrderEntityQueryWrapper = new QueryWrapper<>();
            waybillOrderEntityQueryWrapper.lambda().eq(WaybillOrderEntity::getWaybillno, shippingInformation.getId());
            WaybillOrderEntity waybillOrderEntity = waybillOrderService.getOne(waybillOrderEntityQueryWrapper);
            if (waybillOrderEntity==null){
                return R.error();
            }
            logisticsTrackingEntity.setAddress(String.valueOf(waybillOrderEntity.getSendprovince()).concat(StringPool.COMMA).concat(String.valueOf(waybillOrderEntity.getSendcity())).concat(StringPool.COMMA).concat(String.valueOf(waybillOrderEntity.getSenddistrict())));
            logisticsTrackingEntity.setCreateby(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername());
            logisticsTrackingService.save(logisticsTrackingEntity);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:shippinginformation:update")
    public R update(@RequestBody ShippingInformationEntity shippingInformation){
		shippingInformationService.updateById(shippingInformation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:shippinginformation:delete")
    public R delete(@RequestBody Long[] ids){
		shippingInformationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
